<?php
declare (strict_types = 1);

namespace app\admin\controller\v1\pageConfig;
use Throwable;
use app\common\Backend;
use app\common\model\FooterNav;
use app\common\model\Navigation;
use general\Tree;
use general\Random;
use think\Request;

class FooterNavCon extends Backend
{

   
     /**
     * @var Navigation
     */
    protected $model = null;

    protected $quickSearchField = ['title'];

    /**
     * 远程select初始化传值
     * @var array
     */
    protected $initValue;
    protected $tree = null;
    /**
     * 搜索关键词
     * @var array|string
     */
    protected $keyword = false;

    /**
     * 是否组装Tree
     * @var bool
     */
    protected $assembleTree;

    protected $modelValidate = false;


    public function initialize()
    {
        parent::initialize();
        $this->model = new FooterNav();
        $this->tree  = Tree::instance();

        $isTree          = $this->request->param('isTree', true);
        $this->initValue = $this->request->get("initValue/a", '');
        $this->keyword   = $this->request->request("quick_search");
    
        // 有初始化值时不组装树状（初始化出来的值更好看）
        $this->assembleTree = $isTree && !$this->initValue;
    }

     /**
     * 数据源
     * @throws Throwable
     */
    public function index(): void
    {
        if ($this->request->param('select')) {
            $this->categorySelect();
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->where($where)
            ->order($order)
            ->paginate($limit);

        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => '',
        ]);
    }


    //获取分类的最顶级
    public function storeCategoryList() : void {

        if ($this->request->param('select')) {
            $this->categorySelect();
        }
        $this->success('', [
            'list'   => $this->getCategoryMenus(),
            'remark' => '',
        ]);
    }

  /**
     * 重写select方法
     * @throws Throwable
     */
    public function categorySelect(): void
    {
        $data = $this->getCategoryMenus([], ['status', '=', '1']);

        if ($this->assembleTree) {
            $data = $this->tree->assembleTree($this->tree->getTreeArray($data, 'title'));
        }
        $this->success('', [
            'options' => $data
        ]);
    }

    //树
    protected function getCategoryMenus($where = []): array
    {
        $pk      = $this->model->getPk();


        $initKey = $this->request->get("initKey/s", $pk);

        $ids = $this->auth->getRuleIds();

        // 如果没有 * 则只获取用户拥有的规则
        if (!in_array('*', $ids)) {
            $where[] = ['id', 'in', $ids];
        }

        if ($this->keyword) {
            $keyword = explode(' ', $this->keyword);
            foreach ($keyword as $item) {
                $where[] = [$this->quickSearchField, 'like', '%' . $item . '%'];
            }
        }

        if ($this->initValue) {
            $where[] = [$initKey, 'in', $this->initValue];
        }
        // 读取用户组所有权限规则
        $rules = $this->model
            ->where($where)
            ->order('id desc')
            ->select()->toArray();
        // 如果要求树状，此处先组装好 children
        return $this->assembleTree ? $this->tree->assembleChild($rules) : $rules;
    }

    


}